import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import Compression from "vite-plugin-compression";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  const isBuild = command === "build";
  const alias = {
    "@/": resolve("src") + "/",
  };
  const plugins = [vue()];
  if (isBuild) {
    // alias["./as-needed"] = "./global-import";
    // // 组件按需引入
    plugins.push(
      Components({
        dts: false,
        resolvers: [
          ElementPlusResolver({
            importStyle: "sass",
          }),
        ],
      })
    );
    // gzip压缩
    plugins.push(
      Compression({
        disable: !isBuild,
        threshold: 10240,
        algorithm: "gzip",
        ext: ".gz",
      })
    );
  } else {
    // 开发环境全局安装
    alias["./as-needed"] = "./global-import";
  }
  return {
    build: {
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: command === "build" && isBuild,
          drop_debugger: command === "build" && isBuild,
        },
      },
    },

    server: {
      port: 9990,
      hmr: true, //开启热更新
      host: true,
      open: true,
      proxy: {
        // https://cn.vitejs.dev/config/#server-proxy
        "/api": {
          target: "http://shzl.test.m3a.top/api",
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/api/, ""),
        },
      },
    },
    resolve: { alias },
    plugins,
  };
});
